<?php
/**
 * 设计模式 -- 策略模式
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-3-24
 * Time: 下午3:43
 */
/**
 * 定义接口
 * Interface IStrategy
 */
interface IStrategy{
    function fileter($record);
}

/**
 * 实现接口方式一
 * Class FindAfterStrategy
 */
class FindAfterStrategy implements IStrategy{

    private $_name = '';
    public function __construct($name){
        $this->_name = $name;
    }

    public function fileter($record){
        return strcmp($this->_name,$record) <= 0;
    }
}

class RandomStrategy implements IStrategy{

    public function fileter($record){
        return rand(0,1) > 0.5;
    }
}

/**
 * 主类
 * Class user_list
 */
class user_list{
    private $_list = array();

    public function __construct($names){
        if($names != null ){
            foreach($names as $val){
                $this->_list[] = $val;
            }
        }
    }

    public function add($name){
        $this->_list[] =  $name;
    }

    public function find($filter){
        $recs = array();
        foreach($this->_list as $val){
            if($filter->fileter($val)){
                $recs[] = $val;
            }
        }
        return $recs;
    }

}


$arr = array('Andy','Henly','Jack','John','black','white');
$user = new user_list($arr);
$data =  $user->find(new FindAfterStrategy('j'));
$data2 =  $user->find(new RandomStrategy());
echo '<pre>';
var_dump($data);
var_dump($data2);